﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Web;

namespace dreametryVersion2
{
    public partial class frmTaskDetails : Form
    {
        public frmTaskDetails()
        {
            InitializeComponent();
        }
        //
        String refLink;
        //borderless form drag
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;
        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        int taskId;

        private void frmTaskDetails_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        public void displayData(int id, String creator, String taskname, String description, String flow, String logs, String link, DateTime created, DateTime updated)
        {
            this.Text = taskname;
            lblCreated.Text = "Created on " + created.ToString("dddd MMMM dd, yyyy hh:mm tt") + Environment.NewLine + "Current Status: " +flow;
            txtTitle.Text = taskname;
            lblDescription.Text = description;
            taskId = id;
            lblTitle.Text ="Task Number: " + id.ToString();

            //txtComments.AppendText(comments);
            refLink = link;
            
        }

        private void btnDetails_Click(object sender, EventArgs e)
        {
            pnlDetails.Visible = true;
            pnlComments.Visible = false;
            pnlOutput.Visible = false;
            pnlAddCollaborator.Visible = false;
            
            btnDetails.BackColor = ColorTranslator.FromHtml(global.darkerColor(global.themeColor));
            btnComments.BackColor = Color.WhiteSmoke;
            btnOutput.BackColor = Color.WhiteSmoke;
            btnAddCollaborator.BackColor = Color.WhiteSmoke;
            
            btnDetails.ForeColor = Color.White;
            btnComments.ForeColor = Color.FromArgb(64, 64, 64);
            btnOutput.ForeColor = Color.FromArgb(64, 64, 64);
            btnAddCollaborator.ForeColor = Color.FromArgb(64, 64, 64);
        }

        private void btnComments_Click(object sender, EventArgs e)
        {
            pnlDetails.Visible = false;
            pnlComments.Visible = true;
            pnlOutput.Visible = false;
            pnlAddCollaborator.Visible = false;

            this.ucComments1.getComment(taskId);
            this.ucComments1.Focus();
            btnDetails.BackColor = Color.WhiteSmoke;
            btnComments.BackColor = ColorTranslator.FromHtml(global.darkerColor(global.themeColor));
            btnOutput.BackColor = Color.WhiteSmoke;
            btnAddCollaborator.BackColor = Color.WhiteSmoke;

            btnDetails.ForeColor = Color.FromArgb(64, 64, 64);
            btnComments.ForeColor = Color.White;
            btnOutput.ForeColor = Color.FromArgb(64, 64, 64);
            btnAddCollaborator.ForeColor = Color.FromArgb(64, 64, 64);
        }

        private void frmTaskDetails_Load(object sender, EventArgs e)
        {
            this.BackColor = ColorTranslator.FromHtml(global.themeColor);
            pnlTitleHeader.BackColor = ColorTranslator.FromHtml(global.themeColor);
            txtTitle.BackColor = ColorTranslator.FromHtml(global.themeColor);
            btnAddComment.BackColor = ColorTranslator.FromHtml(global.themeColor);
            btnAdd.BackColor = ColorTranslator.FromHtml(global.themeColor);
            btnDetails.PerformClick();

            //String x = System.Net.WebUtility.HtmlDecode("<a href='www.google.com'>my html content</a>");
            //richTextBox1.Text = x;
        }

        private void btnLink_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(refLink);
        }

        private void btnExit_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnMinimize_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void btnAddComment_Click(object sender, EventArgs e)
        {
            string commandText = "INSERT INTO CommentsTable (emp_id, task_id, comment) VALUES(@empid, @task, @comment)";

            using (SqlConnection connection = new SqlConnection(global.conString))
            {
                SqlCommand command = new SqlCommand(commandText, connection);
                command.CommandType = CommandType.Text;
                command.Parameters.AddWithValue("@comment", txtComment.Text);
                command.Parameters.AddWithValue("@empid", global.employeeID);
                command.Parameters.AddWithValue("@task", taskId);
                try
                {
                    command.Connection.Open();
                    command.ExecuteNonQuery();
                }
                catch
                {

                }
                finally
                {
                    command.Connection.Close();
                }


            }

            txtComment.Clear();
            this.ucComments1.getComment(taskId);
        }

        //resize
        protected override void WndProc(ref Message m)
        {
            const int RESIZE_HANDLE_SIZE = 10;

            switch (m.Msg)
            {
                case 0x0084/*NCHITTEST*/ :
                    base.WndProc(ref m);

                    if ((int)m.Result == 0x01/*HTCLIENT*/)
                    {
                        Point screenPoint = new Point(m.LParam.ToInt32());
                        Point clientPoint = this.PointToClient(screenPoint);
                        if (clientPoint.Y <= RESIZE_HANDLE_SIZE)
                        {
                            if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                                m.Result = (IntPtr)13/*HTTOPLEFT*/ ;
                            else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                                m.Result = (IntPtr)12/*HTTOP*/ ;
                            else
                                m.Result = (IntPtr)14/*HTTOPRIGHT*/ ;
                        }
                        else if (clientPoint.Y <= (Size.Height - RESIZE_HANDLE_SIZE))
                        {
                            if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                                m.Result = (IntPtr)10/*HTLEFT*/ ;
                            else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                                m.Result = (IntPtr)2/*HTCAPTION*/ ;
                            else
                                m.Result = (IntPtr)11/*HTRIGHT*/ ;
                        }
                        else
                        {
                            if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                                m.Result = (IntPtr)16/*HTBOTTOMLEFT*/ ;
                            else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                                m.Result = (IntPtr)15/*HTBOTTOM*/ ;
                            else
                                m.Result = (IntPtr)17/*HTBOTTOMRIGHT*/ ;
                        }
                    }
                    return;
            }
            base.WndProc(ref m);
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.Style |= 0x20000; // <--- use 0x20000
                return cp;
            }
        }

        private void btnOutput_Click(object sender, EventArgs e)
        {
            pnlDetails.Visible = false;
            pnlComments.Visible = false;
            pnlOutput.Visible = true;
            pnlAddCollaborator.Visible = false;

            btnDetails.BackColor = Color.WhiteSmoke;
            btnComments.BackColor = Color.WhiteSmoke;
            btnOutput.BackColor = ColorTranslator.FromHtml(global.darkerColor(global.themeColor));
            btnAddCollaborator.BackColor = Color.WhiteSmoke;

            btnDetails.ForeColor = Color.FromArgb(64, 64, 64);
            btnComments.ForeColor = Color.FromArgb(64, 64, 64);
            btnOutput.ForeColor = Color.White;
            btnAddCollaborator.ForeColor = Color.FromArgb(64, 64, 64);


            string comString = "SELECT * FROM ViewEmployeeTaskData WHERE task_id ='"+taskId+"'";

            using (SqlConnection con = new SqlConnection(global.conString))
            using (SqlDataAdapter adapter = new SqlDataAdapter(comString, con))
            {
                DreametryDBDataSet ds = new DreametryDBDataSet();
                ds.ViewEmployeeTaskData.Clear();
                ds.EnforceConstraints = false;
                adapter.Fill(ds.ViewEmployeeTaskData);
                taskOutput report = new taskOutput();
                report.SetDataSource(ds);
                report.SetParameterValue("href", "<a href='www.google.com'>test</a>");

                rptOutput.ReportSource = report;

            }

        }

        private void btnAddCollaborator_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();

            pnlDetails.Visible = false;
            pnlComments.Visible = false;
            pnlOutput.Visible = false;
            pnlAddCollaborator.Visible = true;

            btnDetails.BackColor = Color.WhiteSmoke;
            btnComments.BackColor = Color.WhiteSmoke;
            btnOutput.BackColor = Color.WhiteSmoke;
            btnAddCollaborator.BackColor = ColorTranslator.FromHtml(global.darkerColor(global.themeColor));

            btnDetails.ForeColor = Color.FromArgb(64, 64, 64);
            btnComments.ForeColor = Color.FromArgb(64, 64, 64);
            btnOutput.ForeColor = Color.FromArgb(64, 64, 64);
            btnAddCollaborator.ForeColor = Color.White;

            string commandText = "SELECT * FROM employee";

            using (SqlConnection connection = new SqlConnection(global.conString))
            using (SqlDataAdapter adapter = new SqlDataAdapter(commandText, connection))
            {
                DataTable users = new DataTable();
                adapter.Fill(users);

                cmbEmployeeList.DisplayMember = "fullname";
                cmbEmployeeList.ValueMember = "emp_id";
                cmbEmployeeList.DataSource = users;
            }

            using (SqlConnection connection = new SqlConnection(global.conString))
            {
                //sql command and fill data to datatable
                SqlCommand command = new SqlCommand("SELECT ViewEmployeeTaskData.fullname, ViewEmployeeTaskData.part FROM ViewEmployeeTaskData Where ViewEmployeeTaskData.task_id = @id", connection);
                command.Parameters.AddWithValue("@id", taskId);
                command.Connection.Open();

                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable data = new DataTable();
                data.Clear();
                adapter.Fill(data);
                SqlDataReader reader = command.ExecuteReader();
                int count = data.Rows.Count;

                for (int i = 0; i <= count-1; i++)
                {
                    String name = data.Rows[i][0].ToString();
                    String role = data.Rows[i][1].ToString();
                    listBox1.Items.Add(name+" - "+role);
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //colaborate task
            using (SqlConnection conn = new SqlConnection(global.conString))
            {
                conn.Open();
                SqlCommand command = new SqlCommand("CollaborateTask", conn);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@taskId", taskId));
                command.Parameters.Add(new SqlParameter("@empid", cmbEmployeeList.SelectedValue));
                command.ExecuteNonQuery();
            }

            btnAddCollaborator.PerformClick();
        }
    }
}
